/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cms.action;

import cms.sessionbean.ConferenceApplicationsRemote;
import cms.sessionbean.PaymentMgmtRemote;
import cms.util.Constants;
import cms.util.EjbLocator;
import com.opensymphony.xwork2.ActionSupport;
import dto.Conference;
import dto.Participant;
import dto.ParticipantId;
import dto.Timeline;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Gary
 */
public class ConferenceAction  extends ActionSupport{
    private static final long serialVersionUID = 7182215130854270652L;
    private List<Conference> conferences;
    private Conference conference;
    private Timeline  timeline;
    private String redirectURL ; 
    private String errorMessage;
    private List<Participant> participantList;

    public List<Participant> getParticipantList() {
        return participantList;
    }

    public void setParticipantList(List<Participant> participantList) {
        this.participantList = participantList;
    }
            
    private String openFrom, openTo, registerFrom,registerTo;         

    public String getOpenFrom() {
        return openFrom;
    }

    public void setOpenFrom(String openFrom) {
        this.openFrom = openFrom;
    }

    public String getOpenTo() {
        return openTo;
    }

    public void setOpenTo(String openTo) {
        this.openTo = openTo;
    }

    public String getRegisterFrom() {
        return registerFrom;
    }

    public void setRegisterFrom(String registerFrom) {
        this.registerFrom = registerFrom;
    }

    public String getRegisterTo() {
        return registerTo;
    }

    public void setRegisterTo(String registerTo) {
        this.registerTo = registerTo;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public String getRedirectURL() {
        return redirectURL;
    }

    public void setRedirectURL(String redirectURL) {
        this.redirectURL = redirectURL;
    }

    public Timeline getTimeline() {
        return timeline;
    }

    public void setTimeline(Timeline timeline) {
        this.timeline = timeline;
    }

    public boolean isIsUpdate() {
        return isUpdate;
    }

    public void setIsUpdate(boolean isUpdate) {
        this.isUpdate = isUpdate;
    }
    private boolean isUpdate;
    private String conferenceName;

    public ConferenceAction() {
        
    }
    
   public String execute() throws Exception {
	return Constants.SUCCESS;
    }
   
   
   public List<Conference> getConferences() {
        return conferences;
    }

    public void setConferences(List<Conference> conferences) {
        this.conferences = conferences;
    }

    public Conference getConference() {
        return conference;
    }

    public void setConference(Conference conference) {
        this.conference = conference;
    }

    public String getConferenceName() {
        return conferenceName;
    }

    public void setConferenceName(String conferenceName) {
        this.conferenceName = conferenceName;
    }


    public String list() throws Exception {
                ConferenceApplicationsRemote cb = (ConferenceApplicationsRemote)EjbLocator.getInstance().LookUpBeanByName(ConferenceApplicationsRemote.class, "ConferenceApplications");
                conferences = cb.getAllConferences();
		// dummy data
                //conferences = cb.findAllConferences();
//                conferences = new ArrayList<Conference>();
//                for(int i = 0; i < 10; i++){
//                    Conference dc = new Conference("Conference " + i, "Siminar", "Sands Hotel", true, null, true, 99.90 + i*100,128);
//                    boolean add = conferences.add(dc);
//                }
		return "conferencelist";
	}

    public String edit() throws Exception {
		// conference = (find conference by name);
                ConferenceApplicationsRemote cb = (ConferenceApplicationsRemote)EjbLocator.getInstance().LookUpBeanByName(ConferenceApplicationsRemote.class, "ConferenceApplications");
                conference = cb.getConference(conferenceName);
                timeline = cb.getTimeLines(conferenceName);
                if (timeline == null)
                {
                    timeline = new Timeline();
                }
                if (conference != null)
                {
                    isUpdate=  true; 
                }
                
                // dummy data
//                conference = new Conference(conferenceName);
//                timeline= new Timeline();
//		if (conference != null) {
//			isUpdate = true;
//		}
		return "conferenceform";

	}

    public String create() throws Exception {
		isUpdate = false;
		conference = new Conference("newConference");
                conference.setConferencetype(0);
                timeline = new Timeline();
		return "conferenceform";
	}

    public String update() throws Exception {
		
		if (isUpdate) {
			// update conference
		} else {
                        // insert conference
		}
                
                Date conferHoldFrom,conferHoldTo,conferRegisterFrom,conferRegisterTo;
                DateFormat  format = new SimpleDateFormat("mm/dd/yyyy");
                timeline = new Timeline();
                timeline.setConferenceName(conference.getConferenceName());
                timeline.setHoldFrom(format.parse(this.openFrom));
                timeline.setHoldTo(format.parse(this.openTo));
                timeline.setRegisterFrom(format.parse(this.registerFrom));
                timeline.setRegisterTo(format.parse(this.registerTo));
                redirectURL = "list_All";
                Date now = new Date();
                conference.setSubmiteDate(now);
                ConferenceApplicationsRemote cb = (ConferenceApplicationsRemote)EjbLocator.getInstance().LookUpBeanByName(ConferenceApplicationsRemote.class, "ConferenceApplications");
              
                cb.updateConference(conference);
                cb.updateTimeLine(timeline);
                
		return "conferenceupdated";
	}


    public void validate() {
		if (conference != null) {
			if (conference.getConferenceName().trim().length() == 0) {
				LOG.error("conference name is null");
                                addFieldError("conference.conferenceName", "Conference Name is required");
			}
		}
	}
   public String partificate()
   {
       PaymentMgmtRemote cb = (PaymentMgmtRemote)EjbLocator.getInstance().LookUpBeanByName(PaymentMgmtRemote.class, "PaymentMgmt");
        participantList= cb.getConferencesParticipant(this.conferenceName);

       return "partificatelist";
   }

}
